
package net.starwifi.duobao.service.impl;



import net.starwifi.duobao.service.CaptchaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;


@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Resource(name = "imageCaptchaService")
   private com.octo.captcha.service.CaptchaService imageCaptchaService;


    public BufferedImage buildImage(String captchaId) {
       return (BufferedImage) imageCaptchaService.getChallengeForID(captchaId);
       // return null;
    }

    public boolean isValid(String captchaType, String captchaId, String captcha) {
        if (captchaType==null) {
            if (StringUtils.isNotEmpty(captchaId) && StringUtils.isNotEmpty(captcha)) {
                try {
                    return imageCaptchaService.validateResponseForID(captchaId, captcha.toUpperCase());
                } catch (Exception e) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

}
